let house_id = 0
let house_index = 0
Page({
  data: {
    dialogVisible: false,
    houseList: []
  },
  onShow() {
    this.getHouseList()
  },
  // 获取房屋列表数据
  async getHouseList() {
    const { code, data: houseList } = await wx.http.get('/room')
    if (code !== 10000) return wx.utils.toast()
    this.setData({ houseList })
  },
  // 点击侧滑组件中的删除按钮
  clickDeleteBtn(ev) {
    const { position, instance } = ev.detail
    if (position === 'right') {
      this.setData({
        dialogVisible: true,
      })
      // 待删除的房屋id和索引
     house_id = ev.mark.id
     house_index = ev.mark.index
      instance.close()
    }
  },
  // 确认框关闭
  async dialogClose(ev) {
    // 点击确认，调用删除接口
    if (ev.detail === 'confirm') {
      const { code } = await wx.http.delete('/room/' + house_id)
      if (code !== 10000) return wx.utils.toast()
      this.data.houseList.splice(house_index, 1)
      this.setData({
        houseList: this.data.houseList
      })
    }
  },
  goDetail(ev) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + ev.mark.id
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  }
})
